﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace huaguwang.hgmanage.MemberInfo
{
    public partial class MemberInfoManage_E_Activity : System.Web.UI.Page
    {
        #region 固定方法

        readonly D_T_Member_Exp _Dal = new D_T_Member_Exp();

        private void SetBut(int i, Button but)
        {
            switch(i)
            {
                case 1:
                    but.Attributes.Add("onclick", "return GetUpdateCheckDefault();");
                    break;
                case 2:
                    but.Attributes.Add("onclick", "return GetDelCheckDefault();");
                    break;
                case 3:
                    but.Attributes.Add("onclick", "return GetUpdateCheckDefault();");
                    break;
                case 4:
                    but.Attributes.Add("onclick", "return GetUpdateCheckDefault();");
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 查询的实现方法
        /// </summary>
        /// <param name="begin"></param>
        private void SetData(int begin)
        {
            AdminPublic.Try(() =>
            {
                var p1 = ViewState["ID"].Exp_IntTryParse();

                Pager.RecordCount = _Dal.FindActivity(p1, "");
                this.Repeater1.DataSource = _Dal.
                        FindActivity(begin, Pager.PageSize, p1, "");
                this.Repeater1.DataBind();
            });
        }

        #endregion


        protected void Page_Load(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                Pager.PageSize = 10;



                if(!IsPostBack)
                {
                    //初始化权限组下拉框
                    //ControlsInit();
                    //存储主键
                    var id = Request.Params["ID"].Exp_IntTryParse();
                    ViewState.Add("ID", id);
                    if(id > 0)
                    {
                        var model = _Dal.GetModel(id);
                        if(model == null)
                        {
                            this.Exp_PageSkyAlterReplace("MemberInfoManage.aspx", WebUserToolBar1.EditSKey, "找不到相关资料");
                        }
                        else
                        {
                            //SetControlsData(model);
                            SetData(0);
                        }
                    }
                }
                //工具栏初始化
                WebUserToolBar1.EditInit(
                    UserConst.Exp_GetUser,
                    UserConst.Exp_GetButtonList,
                    SetBut,
                    UserConst.Exp_ToolBarErr);
                WebUserToolBar1.ButtonBegin("返回");
            });
        }




        #region 基本的带有查询的事件组合

        /// <summary>
        /// 分页控件的页面事件
        /// </summary>
        /// <param name="src"></param>
        /// <param name="e"></param>
        protected void Pager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            AdminPublic.Try(() =>
            {
                SetData((e.NewPageIndex - 1) * Pager.PageSize);
            });
        }

        /// <summary>
        /// 详细按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button3(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                var sKey = WebUserToolBar1.MainSetSKey();
                var strID = Request.Params["HiddenID"].Exp_Trim();
                Response.Redirect("MemberInfoManage_E_Particular.aspx?ID=" + strID + "&" + sKey, false);
            });
        }
        /// <summary>
        /// 增加按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button0(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                var sKey = WebUserToolBar1.EditSKey;
                Response.Redirect("MemberInfoManage.aspx?" + sKey);
            });
        }

        /// <summary>
        /// 修改按钮的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button1(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                var strID = Request.Params["HiddenID"].Exp_Trim();
                var sKey = WebUserToolBar1.MainSetSKey();
                Response.Redirect("MemberInfoManage_E.aspx?ID=" + strID + "&" + sKey, false);
            });
        }
        /// <summary>
        /// 删除按钮的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void WebUserToolBar1_Button2(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                var strID = Request.Params["HiddenID"].Exp_Trim();
                var intA = strID.Exp_SplitToList(',', Sel => Sel.Exp_IntTryParse()).Where(Sel => Sel > 0).ToList();
                if(intA == null || intA.Count <= 0)
                {
                    this.Exp_PageAlter("没有找到可以删除的信息");
                    return;
                }

                int i = 0;
                foreach(var item in intA)
                {
                    i++;
                    //检测权限
                    //var tempI = _Dal.GetPowerCount(item);
                    //if (tempI > 0)
                    //{
                    //    this.Exp_PageAlter("当前第" + i + "条资料出现在有效权限组中,不能够直接删除");
                    //    return;
                    //}

                }


                var refInt = _Dal.UpdateFlag(intA);
                if(refInt > 0)
                {
                    //this.Exp_PageWriteLog("资料删除成功",
                    //    "删除成功,删除的id:{0},共{1}个,成功删除{2}个".Exp_Format(strID, intA.Count, refInt),
                    //    "删除用户资料",
                    //    true);
                    SetData(0);
                    this.Exp_PageAlter("删除操作成功");
                    return;
                }
                else
                {
                    this.Exp_PageAlter("删除操作失败");
                    return;
                }
            });
            //this.Exp_PageAlter("获得了" + strID);
        }
        #endregion

        //protected void WebUserToolBar1_Button4(object sender, EventArgs e)
        //{
        //    AdminPublic.Try(() =>
        //    {
        //        var strID = "," + Request.Params["HiddenID"].Exp_Trim();
        //        var intA = strID.Exp_SplitToList(',', Sel => Sel.Exp_IntTryParse()).Where(Sel => Sel > 0).ToList();
        //        if(intA == null || intA.Count <= 0)
        //        {
        //            this.Exp_PageAlter("没有找到可以操作的信息");
        //            return;
        //        }

        //        int i = 0;
        //        foreach(var item in intA)
        //        {
        //            i++;
        //            //检测权限
        //            //var tempI = _Dal.GetPowerCount(item);
        //            //if (tempI > 0)
        //            //{
        //            //    this.Exp_PageAlter("当前第" + i + "条资料出现在有效权限组中,不能够直接删除");
        //            //    return;
        //            //}

        //        }


        //        var refInt = _Dal.ResetPassWord("", "");
        //        if(refInt > 0)
        //        {
        //            //    this.Exp_PageWriteLog("密码重置成功",
        //            //        "id:{0},共{1}个,成功操作{2}个".Exp_Format(strID, intA.Count, refInt),
        //            //        "用户密码重置",
        //            //        true);
        //            SetData(0);
        //            this.Exp_PageAlter("操作成功");
        //            return;
        //        }
        //        else
        //        {

        //            this.Exp_PageAlter("操作失败");
        //            return;
        //        }
        //    });
        //}



        protected void WebUserToolBar1_Button41(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                var sKey = WebUserToolBar1.MainSetSKey();
                var strID = Request.Params["HiddenID"].Exp_Trim();
                Response.Redirect("MemberInfoManage_E_Activity.aspx?ID=" + strID + "&" + sKey, false);

            });
        }

        protected void WebUserToolBar1_Button5(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                WebUserToolBar1.MainSetQuery();
                SetData(0);
            });
        }
    }
}
